package com.koudai.reader;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.koudai.reader.android.R;
import com.koudai.reader.db.SingleDAO;
import com.koudai.reader.util.HttpUtils;
import com.koudai.reader.vo.EndUser;
import com.koudai.reader.vo.ReadBookConfig;

public class SplashActivity extends Activity {

    Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        EndUser.getInstance().init(this);
        SingleDAO.getinstance().init(this);
        ReadBookConfig.getInstance().init(this);
        setContentView(R.layout.splash);

        netWorkStatus();
    }
    
    protected void netWorkStatus() {
        boolean netSataus = false;
        ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        cwjManager.getActiveNetworkInfo();

        if (cwjManager.getActiveNetworkInfo() != null) {
            netSataus = cwjManager.getActiveNetworkInfo().isAvailable();
        }

        if (!netSataus) {
            Builder b = new AlertDialog.Builder(this).setTitle("没有可用的网络").setMessage("是否对网络进行设置？");
            b.setPositiveButton("是", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    Intent mIntent = new Intent("/");
                    ComponentName comp = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
                    mIntent.setComponent(comp);
                    mIntent.setAction("android.intent.action.VIEW");

                    startActivityForResult(mIntent, 0); // 如果在设置完成后需要再次进行操作，可以重写操作代码，在这里不再重写
                }
            }).setNeutralButton("否", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.cancel();
                    SplashActivity.this.finish();
                }
            }).show();
        } else {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    
                    try {
                        HttpUtils.requestUserinfo(SplashActivity.this);
                        HttpUtils.searchTop(0);
                        
                    } catch (Exception e) {
                        handler.post(new Runnable() {
                            
                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
 
                                Toast.makeText(SplashActivity.this, "服务器异常，请稍候登录", Toast.LENGTH_LONG).show();
                            }
                        });
                        finish();
                        
                        e.printStackTrace();
                        return;
                    }
                    handler.post(new Runnable() {
                        
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            Intent intent = new Intent();
                            intent.setClass(SplashActivity.this, MainTabActivity.class);
                            SplashActivity.this.startActivity(intent);
                            SplashActivity.this.finish();
                        }
                    });
                    
                }
            }).start();
        }

    }
}
